//
//  WelcomeViewController.swift
//  仿一个新浪微博
//
//  Created by 孙家林 on 15/9/17.
//  Copyright © 2015年 sunjialin. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    
    ///图像底部约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        ///加载用户头像
        if let urlString = UserAccount.loadAccount()?.avatar_large {
            
            ///更新图像，会自动更新imageView的大小
            iconView.sd_setImageWithURL(NSURL(string:urlString))
        }
    }
    
    ///准备界面
    private func prepareUI() {
    
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        ///自动布局
        ///背景图片
//        backImageView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImageView]))
        backImageView.ff_Fill(view)
        
        ///头像
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
        
        ///记录底部约束
//        iconBottomCons = view.constraints.last
        
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        
        iconBottomCons = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        ///消息文字
//        label.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
    
    ///欢迎动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        /// 提示：修改约束不会立即生效，添加了一个标记，统一由自动布局系统更新约束
        iconBottomCons?.constant = -UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        
        ///动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
                ///强制更新约束
                self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //发送通知，切换控制器
                NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewControllerSwitchNotification, object: true)
        }
    }
    
    ///懒加载控件
    ///背景图片
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///用户头像
    private lazy var iconView:UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imageView.layer.masksToBounds = true
        
        imageView.layer.cornerRadius = 45
        
        return imageView
    }()
    
    ///消息文字
    private lazy var label:UILabel = {
        
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        label.sizeToFit()
        
        return label
    }()
}
